package com.lepu.app.fun.my.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.utils.UmengEvent;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.main.AppManager;
import com.core.lib.utils.main.DateUtilBase;
import com.core.lib.utils.main.UmengHelper;
import com.core.lib.utils.main.UtilityBase;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.lepu.app.fun.index.bean.BeanIndexNew;
import com.lepu.app.fun.my.MyArticleActivity;
import com.lepu.app.fun.my.MyArticleInfoActivity;
import com.lepu.app.fun.my.bean.BeanMyArticle;
import com.lepu.pasm.R;

public class AdapterMyArticle extends BaseAdapter {

	private LayoutInflater mLayoutInflater = null;
	private Context mContext = null;
	private ArrayList<BeanMyArticle> mArrayList = null;
	private int mArticleType = 1;  // 25.精选  19.用药  20.饮食  21.运动  22.心理  23.生活  30.收藏 40 服务推荐用药

	public AdapterMyArticle(Context context,
			ArrayList<BeanMyArticle> arrayList, int articleType) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
		mArticleType = articleType;
	}
	
	public AdapterMyArticle(Context context,
			ArrayList<BeanMyArticle> arrayList, boolean isCollect) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
		
		if(isCollect){
			mArticleType =30;
		}else{
			mArticleType =1;
		}
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;

		if (convertView == null) {
			convertView = mLayoutInflater.inflate(
					R.layout.my_article_activity_listitem, null);

			holder = new ViewHolder();

			LinearLayout mainItemLayout = (LinearLayout) convertView
					.findViewById(R.id.mainItemLayout);
			ImageView iconImageView = (ImageView) convertView
					.findViewById(R.id.iconImageView);
			TextView typeTextView = (TextView) convertView
					.findViewById(R.id.typeTextView);
			TextView titleTextView = (TextView) convertView
					.findViewById(R.id.titleTextView);
			TextView timeTextView = (TextView) convertView
					.findViewById(R.id.timeTextView);
			TextView readTextView = (TextView) convertView
					.findViewById(R.id.readTextView);
			TextView likeTextView = (TextView) convertView
					.findViewById(R.id.likeTextView);

			holder.mainItemLayout = mainItemLayout;
			holder.iconImageView = iconImageView;
			holder.typeTextView = typeTextView;
			holder.titleTextView = titleTextView;
			holder.timeTextView = timeTextView;
			holder.readTextView = readTextView;
			holder.likeTextView = likeTextView;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final BeanMyArticle item = mArrayList.get(position);
		UniversalImageLoadTool.disPlay(item.Cover, holder.iconImageView,
				R.drawable.default_icon_small);

		String tag = "";
		String title = "";
		String time = "";

		if (mArticleType == 30) {//收藏
			tag = item.Tag;
			title = item.Title;
			time = DateUtilBase.stringFromDate(DateUtilBase.dateFromString(
					item.RecordCreateTime, DateUtilBase.YEAR_MONTH_DAY),
					"MM月dd日");

			holder.readTextView.setVisibility(View.GONE);
			holder.likeTextView.setVisibility(View.GONE);

			holder.mainItemLayout
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							
							MyArticleActivity instance = (MyArticleActivity) AppManager.getAppManager().getActivity(MyArticleActivity.class);
							if(instance != null)
							{
								instance.mCurrentBeanMyArticle = item;
								instance.mCurrentOpePosition = position;
								
								Intent intent = new Intent(mContext,
										MyArticleInfoActivity.class);
								intent.putExtra("ArticleID", item.TargetID);
								intent.putExtra("position",position);
								instance.startActivity(intent, true);
							}
						}
					});
		} else {
			tag = item.ArticleTag;
			title = item.ArticleTitle;

			try {
				time = DateUtilBase.stringFromDate(DateUtilBase.dateFromString(
						item.PublishDate, "yyyy年MM月dd日"),
						"MM月dd日");
			} catch (Exception e) {
				time = item.PublishDate;
			}

			holder.readTextView.setVisibility(View.VISIBLE);
			holder.likeTextView.setVisibility(View.VISIBLE);

			holder.readTextView.setText("· 阅读  " + item.ViewCount + "");
			holder.likeTextView.setText("· 点赞  " + item.FavourCount + "");

			// title read textcolor
			if (BeanIndexNew.isExist(item.ArticleID)) {
				holder.titleTextView.setTextColor(mContext.getResources()
						.getColor(R.color.color_text_black2));
			} else {
				holder.titleTextView.setTextColor(mContext.getResources()
						.getColor(R.color.color_text_black));
			}

			holder.mainItemLayout
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {

							if (mArticleType == 40) {
								UmengHelper.CustomEvent(mContext, UmengEvent.event_fuwu_fuwudongtai);
							} else {
								UmengHelper.CustomEvent(mContext, UmengEvent.event_baike_article);
							}

							String ArticleID = "";
							int acid = UtilityBase.parseInt(item.ACID);
							if (acid == 25) {
								//精选
								ArticleID = item.LinkTargetID;
							} else {
								ArticleID = item.ArticleID;
							}

							BaseFragmentActivity activity = (BaseFragmentActivity) mContext;
							Intent intent = new Intent(mContext,
									MyArticleInfoActivity.class);
							intent.putExtra("ArticleID", ArticleID);
							intent.putExtra("position", position);
							activity.startActivity(intent, true);

							BeanIndexNew.insertTargetId(ArticleID);
							holder.titleTextView.setTextColor(mContext.getResources().getColor(R.color.color_text_black2));
						}
					});
		}

		
		// 板块
		if(mArticleType == 25 || mArticleType == 30){  //精选、收藏显示type
			holder.typeTextView.setVisibility(View.VISIBLE);
		}else{
			holder.typeTextView.setVisibility(View.GONE);
		}
		
		holder.typeTextView.setText(tag);
		holder.typeTextView.setSelected(true);
		
		if(tag == null || tag.equals("")){
			holder.typeTextView.setVisibility(View.GONE);
		} else if(tag.equals("精选")) {
			holder.typeTextView.setTextColor(mContext.getResources().getColor(R.color.color_my_default));
			holder.typeTextView.setBackgroundResource(R.drawable.app_text_selector1);
		} else if (tag.equals("用药")) {
			holder.typeTextView.setTextColor(mContext.getResources().getColor(R.color.color_my_medicion));
			holder.typeTextView.setBackgroundResource(R.drawable.app_text_selector2);
		} else if (tag.equals("饮食")) {
			holder.typeTextView.setTextColor(mContext.getResources().getColor(R.color.color_my_food));
			holder.typeTextView.setBackgroundResource(R.drawable.app_text_selector3);
		} else if (tag.equals("运动")) {
			holder.typeTextView.setTextColor(mContext.getResources().getColor(R.color.color_my_sport));
			holder.typeTextView.setBackgroundResource(R.drawable.app_text_selector4);
		} else if (tag.equals("心理")) {
			holder.typeTextView.setTextColor(mContext.getResources().getColor(R.color.color_my_heart));
			holder.typeTextView.setBackgroundResource(R.drawable.app_text_selector5);
		} else if (tag.equals("生活")) {
			holder.typeTextView.setTextColor(mContext.getResources().getColor(R.color.color_my_live));
			holder.typeTextView.setBackgroundResource(R.drawable.app_text_selector6);
		} else if(tag.contains("视频")){
			holder.typeTextView.setTextColor(mContext.getResources().getColor(R.color.color_my_video));
			holder.typeTextView.setBackgroundResource(R.drawable.app_text_selector7);
		}else{
			holder.typeTextView.setVisibility(View.GONE);
		}

		if(mArticleType == 40)
		{
			holder.typeTextView.setVisibility(View.GONE);

			RelativeLayout.LayoutParams param = (RelativeLayout.LayoutParams) holder.titleTextView.getLayoutParams();
			param.topMargin = 30;
			holder.titleTextView.setLayoutParams(param);
		}

		holder.titleTextView.setText(title);
		holder.timeTextView.setText(time);

		// 长按监听
		holder.mainItemLayout.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				// 必须空实现，否则外部长按监听不起作用
				return false;
			}
		});

		return convertView;
	}

	private static class ViewHolder {
		LinearLayout mainItemLayout;
		ImageView iconImageView;
		TextView typeTextView;
		TextView titleTextView;
		TextView timeTextView;
		TextView readTextView;
		TextView likeTextView;
	}
}
